Delphi code for exporting a report

The code below assumes that a PrintJob is already open. The ExportReport procedure takes three parameters: Export FileType, Export Destination, and a PromptForOptions boolean. Note that various other variables are used in the procedure which you may or may not use depending on the Export FileType and Destination specified. These are all listed below in the public variables section.

Calls used
  PEGetExportOptions
  PEExportTo
  PEStartPrintJob
Code
uses CRDelphi;

type
  TCrExportType = (Records, TabSeparated, Ascii, Dif, Csv, 
CharacterSeparated,     TabSeparatedText, CrystalReportRPT, Excel2, 
Excel3, Excel4, LotusWK1,     LotusWK3, LotusWKS, RTF, WordForWindows, 
Excel5, HTML30, HTML32ext,     HTML32std, ODBCTable, PaginatedText, 
Excel5Extended, ReportDefinition);

  TCrExportDestination = (toFile, toEmailViaMapi, toEmailViaVIM, 
toMSExchange, toApplication);

var
  {FileName is required for all Exporting except when going to ODBCTable}
  FFileName              : string[255];
  {Number and Date format need to be specified when going to Records, 
TabSeparated, Dif, Csv, CharacterSeparated}
  FUseRptNumberFmt       : boolean;
  FUseRptDateFmt         : boolean;
  {Quote and Separator need to be specified when going to 
CharacterSeparated}
  FCharSepQuote          : string[1];
  FCharSepSeparator      : string[16];
  {These four need to be specified when going to ODBCTable}
  FODBCSource            : string;
  FODBCUser              : string;
  FODBCPassword          : string;
  FODBCTable             : string;
  {LinesPerPage needs to be specified when going to PaginatedText}
  FLinesPerPage          : Word;
  {These 5 need to be specified when going to Excel5Extended}
  FExcelColumnHeadings   : Bool;
  FExcelConstColumnWidth : Bool;
  FExcelConstant         : double;
  FExcelAreaCode         : smallint; {A SectionCode}
  FExcelTabularFormat    : Bool;
  {If Destination is toEmailViaMapi or toEmailViaVIM these must be filled 
in}
  FEmailToList           : string;
  FEmailCCList           : string;
  FEmailVIMBCCList       : string;
  FEmailSubject          : string;
  FEmailMessage          : string;
  {If Destination is toMSExchange these must be filled in}
  FExchangeProfile       : string;
  FExchangePassword      : string;
  FExchangeFolder        : string;


procedure ExportReport(FileType: TCrExportType; Destination: 
TCrDestination; PromptForOptions: boolean);
type
  TFormat = (DifFormat, Rec, CommaSep, CharSep, HTML, ODBCType,
     PagText, Xls5Ext);
const
  UXFFormatType: array[TCrExportType] of Word =(
    UXFRecordType, UXFTabSeparatedType, UXFTextType, UXFDifType,
    UXFCommaSeparatedType, UXFCharSeparatedType, UXFTabbedTextType,
    UXFCrystalReportType, UXFXls2Type, UXFXls3Type, UXFXls4Type,
    UXFLotusWk1Type, UXFLotusWk3Type, UXFLotusWksType, 
UXFRichTextFormatType,
    UXFWordWinType, UXFXls5Type, UXFHTML3Type, UXFHTML32ExtType, 
UXFHTML32StdType,
    UXFODBCType, UXFPaginatedTextType, UXFXls5TypeExt, 
UXFReportDefinitionType);

  FormatDLL: array[TCrExportType] of string[12] =
   ('u2frec.dll', 'u2fsepv.dll', 'u2ftext.dll', 'u2fdif.dll', 
'u2fsepv.dll',
    'u2fsepv.dll', 'u2ftext.dll', 'u2fcr.dll', 'u2fxls.dll', 
'u2fxls.dll',
    'u2fxls.dll', 'u2fwks.dll', 'u2fwks.dll', 'u2fwks.dll', 
'u2frtf.dll',
    'u2fwordw.dll', 'u2fxls.dll', 'u2fhtml.dll', 'u2fhtml.dll', 
'u2fhtml.dll',
    'u2fodbc.dll', 'u2ftext', 'u2fxls.dll', 'u2frdef.dll');

  DestinationFormat: array[TCrExportDestination] of Word =
   (UXDDiskType, UXDMAPIType, UXDVIMType, UXDExchFolderType, 
UXDApplicationType);

  DestinationDLL: array[TCrExportDestination] of string[12] =
   ('uxddisk.dll', 'uxdmapi.dll', 'uxdvim.dll', 'uxdpost.dll', 
'uxdapp.dll');

var
  {Variables for Export: these must be in scope when StartPrintJob is 
called.  If StartPrintJob is in another procedure, make these variables 
public}
  ExportOpt    : PEExportOptions;
  {UXF: Format Options}
  UXFDif       : UXFDifOptions;
  UXFRec       : UXFRecordStyleOptions;
  UXFCsv       : UXFCommaTabSeparatedOptions;
  UXFCharSep   : UXFCharSeparatedOptions;
  UXFHTML      : UXFHTML3Options;
  UXFODBC      : UXFODBCOptions;
  UXFPagText   : UXFPaginatedTextOptions;
  UXFXls5Ext   : UXFXlsOptions;
  pFormat      : Pointer; 
  {UXD: Disk Options}
  UXDDisk      : UXDDiskOptions;
  UXDMapi      : UXDMapiOptions;
  UXDVIM       : UXDVIMOptions;
  UXDExch      : UXDPostFolderOptions;
  UXDApp       : UXDApplicationOptions;
  pDisk        : Pointer;

  {Private FormatOptions function}
  function FormatOptions(const Key: TFormat): Pointer;
  var
    nCode  : integer;
    nGroup : integer;
  begin
    Result := nil;
    case Key of
      DifFormat:
        begin
          UXFDif.structSize := SizeOf(UXFDIFOptions);
          UXFDif.useReportNumberFormat := FUseRptNumberFmt;
          UXFDif.useReportDateFormat := FUseRptDateFmt;
          Result := @UXFDif;
        end;

      Rec:
        begin
          UXFRec.structSize := SizeOf(UXFRecordStyleOptions);
          UXFRec.useReportNumberFormat := FUseRptNumberFmt;
          UXFRec.useReportDateFormat := FUseRptDateFmt;
          Result := @UXFRec;
        end;

      CommaSep:
        begin
          UXFCsv.structSize := SizeOf(UXFCommaTabSeparatedOptions);
          UXFCsv.useReportNumberFormat := FUseRptNumberFmt;
          UXFCsv.useReportDateFormat := FUseRptDateFmt;
          Result := @UXFCsv;
        end;

      CharSep:
        begin
          UXFCharSep.structSize := SizeOf(UXFCharSeparatedOptions);
          UXFCharSep.useReportNumberFormat := FUseRptNumberFmt;
          UXFCharSep.useReportDateFormat := FUseRptDateFmt;
          UXFCharSep.stringDelimiter := FCharSepQuote[1];
          FCharSepSeparator := FCharSepSeparator + #0;
          UXFCharSep.fieldDelimiter := @FCharSepSeparator[1];
          Result := @UXFCharSep;
        end;

      HTML:
        begin
          UXFHTML.structSize := SizeOf(UXFHTML3Options);
          FFileName := FFileName + #0;
          UXFHTML.filename := @FFileName[1];
          Result := @UXFHTML;
        end;

      ODBCType:
        begin
          UXFODBC.structsize := SizeOf(UXFODBCOptions);
          FODBCSource := FODBCSource + #0;
          UXFODBC.dataSourceName := @FODBCSource[1];
          FODBCUser := FODBCUser + #0;
          UXFODBC.dataSourceUserID := @FODBCUser[1];
          FODBCPassword := FODBCPassword + #0;
          UXFODBC.dataSourcePassword :=  @FODBCPassword[1];
          FODBCTable := FODBCTable + #0;
          UXFODBC.exportTableName := @FODBCTable[1];
          Result := @UXFODBC
        end;

      PagText:
        begin
          UXFPagText.structSize := SizeOf(UXFPaginatedTextOptions);
          UXFPagText.nLinesPerPage := FLinesPerPage;
          Result := @UXFPagText;
        end;

      Xls5Ext:
        begin
          UXFXls5Ext.structSize := SizeOf(UXFXlsOptions);
          UXFXls5Ext.bColumnHeadings := FExcelColumnHeadings;
          UXFXls5Ext.bUseConstColWidth := FExcelConstColumnWidth;
          UXFXls5Ext.fConstColWidth := FExcelConstant;
          nCode := FExcelArea;
          nGroup := nCode mod 25;
          nCode := (nCode div 1000);
          UXFXls5Ext.baseAreaType := nCode;
          UXFXls5Ext.baseAreaGroupNum := nGroup + 1;
          UXFXls5Ext.bTabularFormat := Bool(FExcelTabularFormat);
          Result := @UXFXls5Ext;
        end;
    end;
  end; {FormatOptions}

  {Private DiskOptions function}
  function DiskOptions(const Key : TCrExportDestination) : Pointer;
  begin
    Result := nil;
    case Key of
      {Disk}
      toFile :
        begin
          UXDDisk.structSize := SizeOf(UXDDiskOptions);
          FFileName := FFileName + #0;
          UXDDisk.filename := @FFileName[1];
          Result := @UXDDisk;
        end;
      {MAPI}
      toEmailViaMapi :
        begin
          UXDMapi.structSize := SizeOf(UXDMAPIOptions);
          FEmailToList := FEmailToList + #0;
          UXDMapi.toList := @FEmailToList[1];
          FEmailCCList := FEmailCCList + #0;
          UXDMapi.ccList := @FEmailCCList[1];
          FEmailSubject := FEmailSubject + #0;
          UXDMapi.subject := @FEmailSubject[1];
          FEmailMessage := FEmailMessage + #0;
          UXDMapi.lpmessage := @FEmailMessage[1];
          Result := @UXDMapi;
        end;
      {VIM}
      toEMailViaVIM :
        begin
          UXDVIM.structSize := SizeOf(UXDVIMOptions);
          FEmailToList := FEmailToList + #0;
          UXDVIM.toList := @FEmailToList[1];
          FEmailCCList := FEmailCCList + #0;
          UXDVIM.bccList := @FEmailCCList[1];
          FEmailVIMBCCList := FEmailVIMBCCList + #0;
          UXDVIM.ccList := @FEmailVIMBCCList[1];
          FEmailSubject := FEmailSubject + #0;
          UXDVIM.subject := @FEmailSubject[1];
          FEmailMessage := FEmailMessage + #0;
          UXDVIM.lpmessage := @FEmailMessage[1];
          Result := @UXDVIM;
        end;
      {Exchange}
      toMSExchange :
        begin
          UXDExch.structsize := SizeOf(UXDPostFolderOptions);
          FExchangeProfile := FExchangeProfile + #0;
          UXDExch.pszProfile := @FExchangeProfile[1];
          FExchangePassword := FExchangePassword + #0;
          UXDExch.pszPassword := @FExchangePassword[1];
          FExchangeFolder := FExchangeFolder + #0;
          UXDExch.pszFolderPath := @FExchangeFolder[1];
          UXDExch.wDestType := 1011; {UXDPostDocMessage}
          Result := @UXDExch;
        end;
      {Application}
      toApplication :
        begin
          UXDApp.structSize := SizeOf(UXDApplicationOptions);
          FFileName := FFileName + #0;
          UXDApp.fileName := @FFileName[1];
          Result := @UXDApp;
        end;
    end; 
  end; {DiskOptions}

{Main procedure: ExportReport}
begin
  ExportOpt.structSize := SizeOf(PEExportOptions);

  {The Print Engine can prompt for Export Options if desired}
  if PromptForOptions then
  begin
    if not PEGetExportOptions(PrintJob, ExportOpt) then
      {Do Error Handler};
  end
  else
  begin
    {Set FormatOptions}
    pFormat := nil;
    UXFCharSep.fieldDelimiter := nil;
    case FileType of
      Records                  : pFormat := FormatOptions(Rec);
      TabSeparated             : pFormat := FormatOptions(CommaSep);
      Ascii                    : pFormat := nil;
      Dif                      : pFormat := FormatOptions(DifFormat);
      Csv                      : pFormat := FormatOptions(CommaSep);
      CharacterSeparated       : pFormat := FormatOptions(CharSep);
      TabSeparatedText..Excel5 : pFormat := nil;
      HTML30..HTML32std        : pFormat := FormatOptions(HTML);
      ODBCTable                : pFormat := FormatOptions(ODBCType);
      PaginatedText            : pFormat := FormatOptions(PagText);
      Excel5Extended           : pFormat := FormatOptions(Xls5Ext);
      ReportDefinition         : pFormat := nil;
    end; 
    StrPCopy(@ExportOpt.formatDLLName, FormatDLL[FileType]);
    ExportOpt.formatType := UXFFormatType[FileType];
    ExportOpt.formatOptions := pFormat;

    {Set DestinationOptions}
    pDisk := nil;
    if Destination = toApplication then
    begin
      StrPCopy(@ExportOpt.destinationDLLName, 
DestinationDLL[Destination]{uxdapp});
      ExportOpt.destinationType := 
DestinationFormat[Destination]{UXDApplicationType};
      pDisk := DiskOptions(Destination);
      ExportOpt.destinationOptions := pDisk;
    end
    else
    begin
      StrPCopy(@ExportOpt.destinationDLLName, 
DestinationDLL[Destination]);
      ExportOpt.destinationType := DestinationFormat[Destination];
      pDisk := DiskOptions(Destination);
      ExportOpt.destinationOptions := pDisk;
    end;
  end;

  {Send the ExportOptions to the Print Engine}
  if not PEExportTo(PrintJob, ExportOpt) then
    {Do Error Handler};

  {Start Exporting the Report}
  if not PEStartPrintJob(PrintJob, True) then
    {Do Error Handler};
end;



Seagate Software IMG Holdings, Inc.
http://www.seagatesoftware.com
Support services:
http://support.seagatesoftware.com